<template>
  <el-dialog
    :title="!dataForm.fireTime ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="纬度" prop="lat">
      <el-input v-model="dataForm.lat" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="lng">
      <el-input v-model="dataForm.lng" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="地域" prop="area">
      <el-input v-model="dataForm.area" placeholder="地域"></el-input>
    </el-form-item>
    <el-form-item label="volcano" prop="volcano">
      <el-input v-model="dataForm.volcano" placeholder="volcano"></el-input>
    </el-form-item>
    <el-form-item label="fireLevel" prop="fireLevel">
      <el-input v-model="dataForm.fireLevel" placeholder="fireLevel"></el-input>
    </el-form-item>
    <el-form-item label="reliabilty" prop="reliabilty">
      <el-input v-model="dataForm.reliabilty" placeholder="reliabilty"></el-input>
    </el-form-item>
    <el-form-item label="frp" prop="frp">
      <el-input v-model="dataForm.frp" placeholder="frp"></el-input>
    </el-form-item>
    <el-form-item label="qf" prop="qf">
      <el-input v-model="dataForm.qf" placeholder="qf"></el-input>
    </el-form-item>
    <el-form-item label="address" prop="address">
      <el-input v-model="dataForm.address" placeholder="address"></el-input>
    </el-form-item>
    <el-form-item label="addressCode" prop="addressCode">
      <el-input v-model="dataForm.addressCode" placeholder="addressCode"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          fireTime: 0,
          lat: '',
          lng: '',
          area: '',
          volcano: '',
          fireLevel: '',
          reliabilty: '',
          frp: '',
          qf: '',
          address: '',
          addressCode: ''
        },
        dataRule: {
          lat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lng: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          volcano: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          fireLevel: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          reliabilty: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          frp: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          qf: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          addressCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.fireTime = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.fireTime) {
            this.$http({
              url: this.$http.adornUrl(`/generator/fireforecastdata/info/${this.dataForm.fireTime}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.lat = data.fireForecastData.lat
                this.dataForm.lng = data.fireForecastData.lng
                this.dataForm.area = data.fireForecastData.area
                this.dataForm.volcano = data.fireForecastData.volcano
                this.dataForm.fireLevel = data.fireForecastData.fireLevel
                this.dataForm.reliabilty = data.fireForecastData.reliabilty
                this.dataForm.frp = data.fireForecastData.frp
                this.dataForm.qf = data.fireForecastData.qf
                this.dataForm.address = data.fireForecastData.address
                this.dataForm.addressCode = data.fireForecastData.addressCode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/fireforecastdata/${!this.dataForm.fireTime ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'fireTime': this.dataForm.fireTime || undefined,
                'lat': this.dataForm.lat,
                'lng': this.dataForm.lng,
                'area': this.dataForm.area,
                'volcano': this.dataForm.volcano,
                'fireLevel': this.dataForm.fireLevel,
                'reliabilty': this.dataForm.reliabilty,
                'frp': this.dataForm.frp,
                'qf': this.dataForm.qf,
                'address': this.dataForm.address,
                'addressCode': this.dataForm.addressCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
